﻿#region copyright
//<copyright>
// Copyright(C) 2012 TrackerRealm Corporation
// This file is part of the open source project - Jazz. http://jazz.codeplex.com
// 
// Jazz is open software: you can redistribute it and/or modify it 
// under the terms of the GNU Affero General Public License (AGPL) as published by 
// the Free Software Foundation, version 3 of the License.
// 
// Jazz is distributed in the hope that it will be useful, 
// but WITHOUT ANY WARRANTY; without even the implied warranty 
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
//  See the GNU Affero General Public License (AGPL) for more details.
// 
// You should have received a copy of the GNU General Public 
// License along with Jazz.  If not, see <http://www.gnu.org/licenses/>.
//
// REMOVAL OF THIS NOTICE IS VIOLATION OF THE COPYRIGHT. 
//</copyright>
#endregion
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel;
using System.Reflection;
using TrackerRealm.Jazz.Common;
using C = TrackerRealm.Jazz.Common.JazzConstants;

// namespace will change for this class
namespace TrackerRealm.Jazz.Client
{
    /// <summary>
    /// A Jazz Role class represents a role.
    /// <para>The role class allows roles to be strongly typed. </para> 
    /// <para>This class can also be inherited allowing for the creation of a
    /// specialized role class that can add extra features to the role.  </para>
    /// <para>For more information see http://wiki.jazzfire.net/role.ashx and
    /// http://wiki.jazzfire.net/Customization.ashx </para>
    /// </summary>
    [Root]
    [Jazz]
    public abstract class jRoleBase : jNameUnique, IjRoleOrProfile
    {
        /// <summary>
        /// 
        /// </summary>
        internal readonly new static jRoleBase[] EmptyArray = new jRoleBase[0];
        //
        // Constructors
        //
        #region Constructor jRole(string name)
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="name"></param>
        protected jRoleBase(string name)
        :this(null, name)
        {
        }
        #endregion
        #region Constructor jRole(ClientNexusBase nexus, string name)
        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="nexus"></param>
        /// <param name="name"></param>
        protected jRoleBase(ClientNexusBase nexus, string name)
            : base(nexus, name)
        {
            roleArray = new jRoleBase[] { this };
        }
        #endregion
        [BaseField]
        private jRoleBase[] roleArray;
        /// <summary>
        /// 
        /// </summary>
        protected IEnumerable<jRoleBase> _ActualRoles
        {
            get { return this.roleArray.Select(r => r); }
            set { this.roleArray = value.ToArray(); }
        }
        /// <summary>
        /// 
        /// </summary>
        protected internal IEnumerable<jRoleBase> _Roles
        {
            get { return this.ResolveRoles(); }
            set
            {
                // need to perform checks - no duplicates
                jRoleBase[] rArray = value.ToArray();
                if (rArray.Distinct().Count() != rArray.Length)
                    throw new ArgumentException("Elements are not unique.", "value");
                if (this.IsBound)
                {
                    rArray.Where(r => !r.IsBound).Foreach(r => this.ClientNexus.Bind(r));
                    if (rArray.FirstOrDefault(r => this.ClientNexus != r.ClientNexus) != null)
                        throw new ArgumentException("Attempting to set a 'role' that is bound to another client nexus.");
                }
                this.roleArray = rArray;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <returns></returns>
        [Jazz(AttributeExclude = true)]
        internal IEnumerable<jRoleBase> ResolveRoles()
        {
            foreach (jRoleBase r in this.roleArray)
            {
                if (r == this) yield return this;
                else
                {
                    foreach (jRoleBase rr in r.ResolveRoles())
                    {
                        yield return rr;
                    }
                }
            }
        }
        //
        // Instance Methods and Properties
        //

        #region Add/Remove role
        /// <summary>
        /// Add a role to this role.
        /// </summary>
        /// <param name="role"></param>
        protected void _AddRole(jRoleBase role)
        {
            this._Roles = this._ActualRoles.AddToUniqueSequence(role);
        }
        /// <summary>
        /// Remove a role.
        /// <para>Removing a role from itself has the effect of disabling that role for all users that have that role.</para>
        /// </summary>
        /// <param name="role"></param>
        protected void _RemoveRole(jRoleBase role)
        {
            this._Roles = this._ActualRoles.RemoveFromSequence(role);
        }
        #endregion
        #region IsRoleValid
        ///// <summary>
        ///// *** Not Implemented ***
        ///// Return true when this role is valid.
        ///// <para>This feature allows roles to have their rights revoked or
        ///// to have features such as being valid up till a certain period of time</para>
        ///// </summary>
        //protected virtual bool IsRoleValid
        //{
        //    get { return true; }
        //}
        #endregion

    }
}
